﻿#include "algworker.h"
#include <string>
#include <QDebug>


AlgWorker::AlgWorker(QObject *parent) : QObject(parent)
{
    memset(m_nNGorOK, 1, sizeof(int)* 100);
}

AlgWorker::~AlgWorker()
{

}



void AlgWorker::Initialize()
{

}

void AlgWorker::start(CameraData m_cImageInfo)
{
    HalconCpp::HObject img;
    img = m_cImageInfo.Hobj;
    int nSave = 0;
    int result = 0;

    result = CCD_Alg(img);
    switch (m_cImageInfo.m_nIndex) {
    case 1:
        nSave = m_swSaveImg.nCCD1;
        break;
    case 2:
        nSave = m_swSaveImg.nCCD2;
        break;
    case 3:
        nSave = m_swSaveImg.nCCD3;
        break;
    case 4:
        nSave = m_swSaveImg.nCCD4;
        break;
    default:
        break;
    }

    m_nNGorOK[m_nResult] = result;
    m_cImageInfo.m_nNGorOK =  result;
    show(m_defect.m_Image_Show,m_cImageInfo.hv_WindowHandle,1);

    if(nSave){
        emit sigSaveImgStart(m_cImageInfo);
    }

    if (m_nModel){
        return;
    }
    if (m_cImageInfo.m_nIndex == 4){
        emit sigCameraResult(m_nResult);
    }
    m_nResult++;
    if (m_nResult == 100)
    {
        m_nResult = 0;
    }
}

void AlgWorker::show(HalconCpp::HObject img, HalconCpp::HTuple hv_WindowHandle, int index)
{   
    HalconCpp::HTuple w, h;
    HalconCpp::GetImageSize(img, &w, &h);
    HalconCpp::SetPart(hv_WindowHandle, 0, 0, h, w);
    HalconCpp::DispObj(img, hv_WindowHandle);
}


//M_Model 1测试高度，2测试长度，0全测量   CCD1 0  CCD2 CCD3 正方形 1  CCD4 0
//Direction 正方形 false   长方形true 梯形 true
//  OutPara    CCD1 4个参数  0 高  1 平行度  2 长度  3 长度平行度    CDD3 0 高度   1 平行度   CDD4 4个参数  0 高  1 平行度  2 长度  3 长度平行度
int AlgWorker::CCD_Alg(HObject img)
{
    HalconCpp::HTuple OutPara;
    switch (m_nAlgDefect) {
    case 1:
        result2 =  m_defect.Defect_detect_CCD14_Z(img,m_defect.m_part);
        break;
    case 2:
         result2 =  m_defect.Defect_detect_CCD2(img,m_defect.m_part);
        break;
    case 3:
         result2 =  m_defect.Defect_detect_CCD3(img,m_defect.m_part);
        break;
    case 4:
         result2 =  m_defect.Defect_detect_CCD14_T(img,m_defect.m_part);
        break;
    default:
        result2 = 0;
        m_defect.m_Image_Show = img;
        break;
    }

    switch (m_nAlgRect) {
    case 1:
        result1 =  m_measureRect.M_Rect(img,0,false,1,OutPara,OutImg);
        break;
    case 2:
        result1 =  m_measureTrapezoid.M_Trapezoid(img,OutPara,OutImg);
        break;
    default:
        result1 = 0;
        break;
    }

    result2 = result2*10;
    return result1 + result2;
}
